﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns.ChainOfResponsibilityPatterns
{
    class ChainOfResponsibilityPattern
    {
        static void Main(string[] args)
        {
            //责任链模式，隔离请求与处理者
            //链条较长时，会存在性能为题
            //一般在setnext的时候，判断链条是否超过阈值，避免性能问题
            Handler handler1 = new ConcreteHandler1();
            Handler handler2 = new ConcreteHandler2();
            Handler handler3 = new ConcreteHandler3();
            handler1.setNext(handler2);
            handler2.setNext(handler3);

            var response = handler1.handleMessage(new Request() { level = new Level() });
        }
    }
    /// <summary>
    /// 优先级
    /// </summary>
    public class Level { }
    /// <summary>
    /// 请求，内含优先级信息
    /// </summary>
    public class Request {
        public Level level { get; set; }
    }
    /// <summary>
    /// 回复
    /// </summary>
    public class Response { }
    /// <summary>
    /// 处理者
    /// </summary>
    public abstract class Handler {
        /// <summary>
        /// 下一个处理者
        /// </summary>
        private Handler nextHandler;
        /// <summary>
        /// 处理信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Response handleMessage(Request request) {
            Response response = null;
            if (this.getHandlerLevel().Equals(request.level))
            {
                response = this.echo(request);
            }
            else {
                if (this.nextHandler != null)
                {
                    response = this.nextHandler.handleMessage(request);
                }
                else
                {
                }
            }
            return response;
        }
        /// <summary>
        /// 设置下一个处理者
        /// </summary>
        /// <param name="_handler"></param>
        public void setNext(Handler _handler) {
            this.nextHandler = _handler;
        }
        /// <summary>
        /// 获取当前处理者优先级
        /// </summary>
        /// <returns></returns>
        protected abstract Level getHandlerLevel();
        /// <summary>
        /// 返回当前处理者的回复信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected abstract Response echo(Request request);
    }

    public class ConcreteHandler1 : Handler
    {
        protected override Response echo(Request request)
        {
            return null;
        }

        protected override Level getHandlerLevel()
        {
            return null;
        }
    }
    public class ConcreteHandler2 : Handler
    {
        protected override Response echo(Request request)
        {
            return null;
        }

        protected override Level getHandlerLevel()
        {
            return null;
        }
    }
    public class ConcreteHandler3 : Handler
    {
        protected override Response echo(Request request)
        {
            return null;
        }

        protected override Level getHandlerLevel()
        {
            return null;
        }
    }
}
